package com.suneee.store.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.context.page.Page;
import com.suneee.context.page.Sort;
import com.suneee.context.page.Sort.Order;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.store.bean.ProductDTO;
import com.suneee.store.dao.SpuDao;
import com.suneee.store.entity.Spu;

@Repository("spuDao")
public class SpuDaoImpl extends GenericDao<Spu, Integer>implements SpuDao {

	@Override
	public Page<Spu> listView(ProductDTO dto) {
		// TODO Auto-generated method stub
		WhereClauseBuilder sbf = this.wrapWhereSQL(dto);
		//this.queryForList(sbf.getSql(),dto, sbf.getArgs());
		//this.executeQuery(sbf.getSql(), dto, sbf.getArgs());
		return this.selectWhere(sbf.getSql(), dto, sbf.getArgs());
	}

	private WhereClauseBuilder wrapWhereSQL(ProductDTO dto){
		//" select * from t_spu where 1=1 "
		WhereClauseBuilder sbf = new WhereClauseBuilder();
		sbf.equals("enterprise_code", dto.getEnterpriseCode());
		sbf.equals("spu_id", dto.getSpuId());
		sbf.contains("title", dto.getTitle());
		sbf.between("create_time", dto.getCreateTimeStart(), dto.getCreateTimeEnd());
		dto.setSort(new Sort("spu_id",Order.DESC));
		dto.setPageNumber(dto.getPageNumber() == 0 ? 1 : dto.getPageNumber());
		dto.setPageSize(dto.getPageSize() == 0 ? 10 : dto.getPageSize());
		return sbf;
	}

	@Override
	public List<Spu> getListBySpecIdAndEnterpriseCode(Integer specId, String enterpriseCode) {
		// TODO Auto-generated method stub
		String sql = "select * from t_spu where enterprise_code = ? "
				+ " and (spec_id_one = ? or spec_id_two = ? or spec_id_three = ? or spec_id_four = ?)";
		Object[] args = {enterpriseCode,specId,specId,specId,specId};
		return this.executeQuery(sql, args);
	}

	@Override
	public boolean isUseSpec(Integer specId) {
		String sql = "select * from t_spu where  spec_id_one = ? or spec_id_two = ? or spec_id_three = ? or spec_id_four = ?";
		Object[] args = {specId,specId,specId,specId};
		List<Spu> list = this.executeQuery(sql, args);
		if(null != list && list.size() > 0){
			return true;
		}
		return false;
	}

	@Override
	public boolean isUseEnterpriseSpec(Integer specId) {
		String sql = "select * from t_enterprise_spec where  spec_id = ? ";
		Object[] args = {specId};
		List<Spu> list = this.executeQuery(sql, args);
		if(null != list && list.size() > 0){
			return true;
		}
		return false;
	}

	@Override
	public Page<Map<String, Object>> mapView(ProductDTO dto) {
		// TODO Auto-generated method stub
		String sql = "SELECT sku.sku_id as skuId,spu.title as title,spu.create_time as createTime,"
				+ "sku.price,sku.stock FROM t_spu spu,t_sku sku "
				+ "WHERE spu.spu_id = sku.spu_id ";
		WhereClauseBuilder sbf = new WhereClauseBuilder(sql);
		sbf.equals("spu.enterprise_code", dto.getEnterpriseCode());
		sbf.equals("sku.sku_id", dto.getSkuId());
		sbf.contains("spu.title", dto.getTitle());
		dto.setSort(new Sort("spu.spu_id",Order.DESC));
		return this.queryForList(sbf.getSql(), dto, sbf.getArgs());
	}
	
}
